import Vue from "vue";
import VueRouter from "vue-router";
const Login = () => import("../views/Login.vue");
const Home = () => import("../views/Home.vue");
const Welcome = () => import("../views/Welcome.vue");
const User = () => import("../views/user/User.vue");
const Rights = () => import("../views/auth/Rights.vue");
const Roles = () => import("../views/auth/Roles.vue");
const Categories = () => import("../views/goods/Categories.vue");
const Params = () => import("../views/goods/Params.vue");
const Goods = () => import("../views/goods/Goods.vue");
const GoodsAdd = () => import("../views/goods/GoodsAdd.vue");
const Orders = () => import("../views/order/Orders.vue");
const Repotrs = () => import("../views/report/Reports.vue");

// import Login from "../views/Login.vue";
// import Home from "../views/Home.vue";
// import Welcome from "../views/Welcome.vue";
// import User from "../views/user/User.vue";
// import Rights from "../views/auth/Rights.vue";
// import Roles from "../views/auth/Roles.vue";
// import Categories from "../views/goods/Categories.vue";
// import Params from "../views/goods/Params.vue";
// import Goods from "../views/goods/Goods.vue";
// import GoodsAdd from "../views/goods/GoodsAdd.vue";
// import Orders from "../views/order/Orders.vue";
// import Repotrs from "../views/report/Reports.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/",
    redirect: "/welcome",
  },
  {
    // home本身是第2级别路由,内部需要嵌套设置多个第3级别路由
    path: "/home",
    component: Home,
    redirect: "/welcome", // 路由重定向 方法一
    children: [
      // { path: "/home", redirect: "/welcome" }, // 路由重定向 方法二
      { path: "/welcome", component: Welcome },
      { path: "/users", component: User },
      { path: "/rights", component: Rights },
      { path: "/roles", component: Roles },
      { path: "/categories", component: Categories },
      { path: "/params", component: Params },
      { path: "/goods", component: Goods },
      { path: "/goods/add", component: GoodsAdd },
      { path: "/orders", component: Orders },
      { path: "/reports", component: Repotrs },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach(function(to, from, next) {
  var token = window.sessionStorage.getItem("token");
  // 1.用户没有登录系统(token===null)
  // 2.该用户想要试图访问登录页面以外的页面(例如/home),强制该用户去登录页面进行登录
  if (token === null && to.path !== "/login") {
    return next("/login");
  }
  next(); // 放行
});

export default router;
